package com.zhiwen.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiwen.common.api.Result;
import com.zhiwen.domain.dto.AnswerDTO;
import com.zhiwen.domain.entity.Answer;
import com.zhiwen.service.AnswerService;
import com.zhiwen.domain.vo.AnswerVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 回答控制器
 */
@Tag(name = "回答管理", description = "回答相关接口")
@RestController
@RequestMapping("/answers")
@RequiredArgsConstructor
public class AnswerController {

    private final AnswerService answerService;

    /**
     * 创建回答
     */
    @Operation(summary = "创建回答", description = "对问题发表新回答，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "创建成功",
                content = @Content(schema = @Schema(implementation = AnswerVO.class)))
    @PostMapping
    @SaCheckLogin
    public Result<AnswerVO> createAnswer(@RequestBody @Valid AnswerDTO answerDTO) {
        AnswerVO answerVO = answerService.createAnswer(answerDTO);
        return Result.success(answerVO);
    }

    /**
     * 更新回答
     */
    @Operation(summary = "更新回答", description = "更新已有回答，需要登录且只有创建者可以更新")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "更新成功",
                content = @Content(schema = @Schema(implementation = AnswerVO.class)))
    @PutMapping("/{id}")
    @SaCheckLogin
    public Result<AnswerVO> updateAnswer(
            @Parameter(description = "回答ID") @PathVariable Long id,
            @RequestBody @Valid AnswerDTO answerDTO) {
        answerDTO.setId(id);
        AnswerVO answerVO = answerService.updateAnswer(answerDTO);
        return Result.success(answerVO);
    }

    /**
     * 删除回答
     */
    @Operation(summary = "删除回答", description = "删除已有回答，需要登录且只有创建者可以删除")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "删除成功")
    @DeleteMapping("/{id}")
    @SaCheckLogin
    public Result<Void> deleteAnswer(@Parameter(description = "回答ID") @PathVariable Long id) {
        answerService.deleteAnswer(id);
        return Result.success();
    }

    /**
     * 获取回答详情
     */
    @Operation(summary = "获取回答详情", description = "根据ID获取回答详情")
    @ApiResponse(responseCode = "200", description = "获取成功",
                content = @Content(schema = @Schema(implementation = AnswerVO.class)))
    @GetMapping("/{id}")
    public Result<AnswerVO> getAnswerDetail(@Parameter(description = "回答ID") @PathVariable Long id) {
        AnswerVO answerVO = answerService.getAnswerDetail(id);
        return Result.success(answerVO);
    }

    /**
     * 获取问题下的回答列表
     */
    @Operation(summary = "获取问题下的回答列表", description = "分页获取指定问题下的回答列表")
    @Parameters({
        @Parameter(name = "questionId", description = "问题ID"),
        @Parameter(name = "current", description = "当前页码", schema = @Schema(defaultValue = "1")),
        @Parameter(name = "size", description = "每页数量", schema = @Schema(defaultValue = "10"))
    })
    @ApiResponse(responseCode = "200", description = "获取成功")
    @GetMapping("/question/{questionId}")
    public Result<Page<AnswerVO>> getAnswerPage(
            @PathVariable Long questionId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Answer> page = new Page<>(current, size);
        Page<AnswerVO> answerPage = answerService.getAnswerPage(questionId, page);
        return Result.success(answerPage);
    }

    /**
     * 采纳回答
     */
    @Operation(summary = "采纳回答", description = "将指定回答标记为已采纳，需要登录且只有问题作者可以采纳")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "采纳成功")
    @PutMapping("/{id}/accept")
    @SaCheckLogin
    public Result<Void> acceptAnswer(@Parameter(description = "回答ID") @PathVariable Long id) {
        answerService.acceptAnswer(id);
        return Result.success();
    }

    /**
     * 点赞回答
     */
    @Operation(summary = "点赞回答", description = "对回答进行点赞，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "点赞成功")
    @PostMapping("/{id}/vote")
    @SaCheckLogin
    public Result<Void> voteAnswer(@Parameter(description = "回答ID") @PathVariable Long id) {
        answerService.voteAnswer(id);
        return Result.success();
    }

    /**
     * 取消点赞
     */
    @Operation(summary = "取消点赞", description = "取消对回答的点赞，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "取消成功")
    @DeleteMapping("/{id}/vote")
    @SaCheckLogin
    public Result<Void> cancelVote(@Parameter(description = "回答ID") @PathVariable Long id) {
        answerService.cancelVote(id);
        return Result.success();
    }
}
